package cn.e4j.modular.erp.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.service.impl.*;
import cn.e4j.modular.erp.mapper.YFMapper;
import cn.e4j.modular.erp.service.Erp;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

/**
 * GP逻辑处理
 * Created by ganjia on 2019-09-18.
 */
@Service
public class YFServiceImpl extends ServiceImpl<YFMapper, GwamT> implements Erp {
    @Autowired
    GwcsTServiceImpl gwcsTService;

    @Autowired
    GwakTServiceImpl gwakTService;

    @Autowired
    MmrwTServiceImpl mmrwTService;

    @Autowired
    MmrxTServiceImpl mmrxTService;

    @Autowired
    GwdfTServiceImpl gwdfTService;


    @Transactional(readOnly = true)
    public List<Dto> getSiteList(Page<Object> page, Dto dto) {
        return this.baseMapper.getSiteList(page, dto);
    }

    /**
     * 获取营运中心名称
     * @param dto
     * @return
     */
    @Transactional(readOnly = true)
    public Dto getAzpFile(Dto dto) {
        Dto outDto = this.baseMapper.getDSCMB(dto);
        outDto.put("AZP01",outDto.getString("MB001"));
        outDto.put("AZP02",outDto.getString("MB002"));
        return outDto;
    }

    /**
     * 正式订单转订单(gwai710)
     */
    public JSONArray transfer710(Dto dDto){
        JSONArray json = new JSONArray();
        Date date = new Date();
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = dDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);
        int rows = 0;
        for (String data : rid) {
            Dto inDto = Dtos.newDto();
            inDto.put("gwcsdocno", data);
            inDto.put("ent", shiroUser.getDBC());
            inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
            List<Dto> pList = this.gwcsTService.listGwcs032(inDto);
            Boolean transFlag = true;
            for (int t = 0; t < pList.size(); t++) {
                List<Dto> gwcs_tPOs = this.gwcsTService.listGwcsInfo(inDto);
                List<Dto> gwcsz_tPOs = this.gwcsTService.listGwcszInfo(inDto);
                inDto.put("gwak001", gwcs_tPOs.get(0).getString("GWAK001"));
                Dto gwakDto = Dtos.newDto();
                Dto adrDto = Dtos.newDto();
                try {
                    gwakDto = this.gwakTService.getGwakData(inDto);
                } catch (Exception e) {
                    JSONObject jo = new JSONObject();
                    jo.put("ERPRESULT", "N");//同步结果  Y  N
                    jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                    jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                    jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                    jo.put("REMARK", "getGwakData：" + e.getMessage());
                    jo.put("GWCS045", "");//订单号
                    json.add(jo);
                    continue;
                }
                try {
                    adrDto = this.gwcsTService.getAddress(inDto);
                } catch (Exception e) {
                    JSONObject jo = new JSONObject();
                    jo.put("ERPRESULT", "N");//同步结果  Y  N
                    jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                    jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                    jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                    jo.put("REMARK", "getAddress：" + e.getMessage());
                    jo.put("GWCS045", "");//订单号
                    json.add(jo);
                    continue;
                }

                //根据订单上的法人组织字段gwcsmlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值。
                Dto dbinfo1 = this.gwcsTService.getDBinfo1(shiroUser.getDBC(), gwcs_tPOs.get(0).getString("GWCSMLEGAL"));// 先查询biz_code_  因数据库排序问题  分成两个语句查询
                dbinfo1.put("ERP_Link",dDto.getString("ERP_Link"));
                dbinfo1.put("ERP_DB",dDto.getString("ERP_DB"));
                Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);
                String dbname = dbinfo2.getString("MB003").trim();

                if (dbname.isEmpty()) { //没查到目标数据库  添加失败
                    JSONObject jo = new JSONObject();
                    jo.put("ERPRESULT", "N");//同步结果  Y  N
                    jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                    jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003"));//客户名称
                    jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                    jo.put("REMARK", "订单没有归属法人");
                    jo.put("GWCS045", "");//订单号
                    json.add(jo);
                    continue;
                }
                Integer count = 1;
                Dto tcDto = Dtos.newDto();
                tcDto.put("TC002", DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMdd));
                tcDto.put("ERP_Link", dDto.getString("ERP_Link"));
                tcDto.put("MB003", dbname);
                String dd01 = this.baseMapper.getMaxTc002(tcDto);
                if (dd01 == null) {
                    dd01 = DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMdd) + "001";
                }

                //判断订单是否已经抛过  都是整单抛转 不需要项次
                Dto inDto3 = Dtos.newDto();
                inDto3.put("ERP_Link", dDto.getString("ERP_Link"));
                inDto3.put("MB003", dbname);
                inDto3.put("UDF11", gwcs_tPOs.get(0).getString("GWCSDOCNO"));
                Dto checkinfo = this.baseMapper.getUDF11(inDto3);
                if (checkinfo != null) {
                    JSONObject jo = new JSONObject();
                    jo.put("ERPRESULT", "N");//同步结果  Y  N
                    jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                    jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                    jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                    jo.put("REMARK", "订单已经抛转");
                    jo.put("GWCS045", "");//订单号
                    json.add(jo);
                    //该订单已经抛转过  同步失败  跳过进行下个订单
                    continue;
                }

                //判断客户编号在ERP中是否存在 不存在就新增一笔数据
                Dto inDto2 = Dtos.newDto();
                Dto outDto2 = Dtos.newDto();
                inDto2.put("ERP_Link", dDto.getString("ERP_Link"));
                inDto2.put("MB003", dbname);
                inDto2.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));
                outDto2 = this.baseMapper.selectByGwak001(inDto2);
                if (outDto2 == null) {
                    String syncresult = this.SyncERP(gwcs_tPOs.get(0).getString("GWAK001"),dDto.getString("ERP_Link"),dbname);
                    if (syncresult.equals("N")) {
                        JSONObject jo = new JSONObject();
                        jo.put("ERPRESULT", "N");//同步结果  Y  N
                        jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                        jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                        jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                        jo.put("REMARK", "同步客户资料失败");
                        jo.put("GWCS045", "");//订单号
                        json.add(jo);
                        //同步客户资料失败  整张订单跳过 进行下个订单
                        continue;
                    }
                }

                try {
                    List<Dto> MD = new ArrayList<>();
                    for (Dto gwcs_tPO : gwcs_tPOs) {
                        //单身信息
                        Dto qDto = Dtos.newDto();

                        qDto.put("ERP_Link",dDto.getString("ERP_Link"));
                        qDto.put("MB003", dbname);//目标数据库
                        //按法人组织抓取单别
                        Dto dhinfo = gwcsTService.getGwaz(shiroUser.getDBC(), gwcs_tPO.getString("GWCSMLEGAL"));
                        qDto.put("TD001", dhinfo.getString("GWAZ002"));
                        qDto.put("TD002", dd01);
                        DecimalFormat mFormat = new DecimalFormat("0000");//确定格式，把1 转换为0001
                        String gwcsseq = mFormat.format(gwcs_tPO.getBigDecimal("GWCSSEQ"));
                        qDto.put("CREATOR", shiroUser.getACCOUNT());
                        qDto.put("USER_GROUP", "");
                        qDto.put("TD003", gwcsseq);
                        qDto.put("TD004", gwcs_tPO.getString("GWCS001"));//取CRM中购物车的品号(需要判断是否在ERP中是否存在?）
                        qDto.put("TD005", gwcs_tPO.getString("GWAML003"));
                        qDto.put("TD006", gwcs_tPO.getString("GWAML004"));
                        qDto.put("TD007", gwcs_tPO.getString("GWCS113"));
                        qDto.put("TD008", gwcs_tPO.getString("GWCS003"));
                        qDto.put("TD009", 0);
                        //qDto.put("TD010", gwcs_tPO.getString("GWCS002"));
                        qDto.put("TD010", gwcs_tPO.getString("GWABL003"));
                        qDto.put("TD011", gwcs_tPO.getString("GWCS115"));
                        qDto.put("TD012", gwcs_tPO.getString("GWCS005"));
                        if (gwcs_tPO.getBigDecimal("GWCS034") == null) {
                            gwcs_tPO.put("GWCS034", 1);//汇率没有默认给1
                        }
                        qDto.put("TDI03", gwcs_tPO.getBigDecimal("GWCS005").multiply(gwcs_tPO.getBigDecimal("GWCS034")));
                        if (gwcs_tPOs.get(0).getDate("GWCS016") != null) {
                            qDto.put("TD013", DateUtils.formatDate(gwcs_tPOs.get(0).getDate("GWCS016"), "yyyyMMdd"));
                        }

                        qDto.put("TD016", "N");
                        qDto.put("TD020", gwcs_tPOs.get(0).getString("GWDC003"));
                        qDto.put("TD021", "Y");
                        qDto.put("TD026", gwcs_tPO.getBigDecimal("GWCS035"));
                        //qDto.put("TD036", gwcs_tPO.getString("GWCS002"));
                        Dto mbDto = Dtos.newDto();
                        mbDto.put("MB003", dbname);
                        mbDto.put("MB001", gwcs_tPO.getString("GWCS001"));
                        mbDto.put("ERP_Link", dDto.getString("ERP_Link"));
                        Dto mb = this.baseMapper.getMB(mbDto);
                        if (mb != null) {
                            qDto.put("TD036", mb.getString("MB090"));//包装单位
                            mbDto.put("TD036", mb.getString("MB090"));
                            Dto tDto = this.baseMapper.getTD032(mbDto);
                            if (mb != null && tDto != null) {
                                //qDto.put("TD032",gwcs_tPO.getBigDecimal("GWCS003").multiply(tDto.getBigDecimal("TD032")));//包装数量
                                qDto.put("TD032", tDto.getBigDecimal("TD032"));//包装数量
                            } else {
                                qDto.put("TD032", 0);
                            }
                        } else {
                            qDto.put("TD032", 0);
                        }

                        if (gwcs_tPO.getBigDecimal("GWEA006") == null || gwcs_tPO.getBigDecimal("GWEA006").equals(new BigDecimal(0.00))) {
                            qDto.put("TD037", 0.16);
                        } else {
                            qDto.put("TD037", gwcs_tPO.getBigDecimal("GWEA006").divide(new BigDecimal(100), 4, RoundingMode.HALF_UP));//根据税种gwcs032抓税率
                        }
                        if (gwcs_tPO.getBigDecimal("TD038") == null) {
                            qDto.put("TD038", "");//(GWCS040/(GWEA006/100)
                        } else {
                            qDto.put("TD038", gwcs_tPO.getBigDecimal("GWCS005").subtract(gwcs_tPO.getBigDecimal("GWCS040").setScale(2, RoundingMode.HALF_UP)));//gwcs036-gwcs040
                            qDto.put("TDI01", (gwcs_tPO.getBigDecimal("GWCS005").subtract(gwcs_tPO.getBigDecimal("GWCS040").setScale(2, RoundingMode.HALF_UP))).multiply(gwcs_tPO.getBigDecimal("GWCS034")));//gwcs036-gwcs040
                        }
                        if (gwcs_tPO.getBigDecimal("GWCS040") == null) {
                            qDto.put("TD039", "");
                        } else {
                            qDto.put("TD039", gwcs_tPO.getBigDecimal("GWCS040").setScale(2, RoundingMode.HALF_UP));
                            qDto.put("TDI02", (gwcs_tPO.getBigDecimal("GWCS040").setScale(2, RoundingMode.HALF_UP)).multiply(gwcs_tPO.getBigDecimal("GWCS034")));
                        }

                        qDto.put("TD048", gwcsseq);
                        qDto.put("TD049", gwcs_tPO.getString("GWCSDOCNO"));
                        //qDto.put("TD050", 0);
                        qDto.put("TD062", "N");
                        //qDto.put("TD200", 0);
                        qDto.put("TDD01", "N");

                        qDto.put("UDF07", qDto.getString("TD004"));
                        qDto.put("UDF61", 0);
                        //qDto.put("UDF62", 0);
                        qDto.put("COMPANY", gwcs_tPO.getString("GWCSPLANT"));
                        //qDto.put("CREATOR", shiroUser.getBIZ_CODE());
                        // qDto.put("USR_GROUP", "");
                        //qDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMdd"));
                        qDto.put("FLAG", 1);
                        //qDto.put("TD021", gwcs_tPO.getString("GWCS050"));

                        inDto.put("gwcs102", count);
                        count++;

                        try {
                            this.baseMapper.insertCOPTD(qDto);
                        } catch (Exception e) {
                            transFlag = false;
                            this.baseMapper.deleteCOPTD(qDto);
                            JSONObject jo = new JSONObject();
                            jo.put("ERPRESULT", "N");//同步结果  Y  N
                            jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                            jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                            jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                            jo.put("GWCS045", "");//订单号
                            jo.put("REMARK", "insertCOPTD：" + e.getMessage());
                            json.add(jo);
                            //插入COPTD 失败  整张订单跳过 进行下个订单
                            break;
                        }

                        inDto.put("gwcs045", dd01);
                        inDto.put("gwcsseq", gwcs_tPO.getString("GWCSSEQ"));
                        inDto.put("gwcsacti", "Z");

                        try {
                            this.gwcsTService.update(inDto);
                        } catch (Exception e) {
                            transFlag = false;
                            JSONObject jo = new JSONObject();
                            jo.put("ERPRESULT", "N");//同步结果  Y  N
                            jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                            jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                            jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                            jo.put("GWCS045", "");//订单号
                            jo.put("REMARK", "update：" + e.getMessage());
                            json.add(jo);
                            //插入COPTD 失败  整张订单跳过 进行下个订单
                            break;
                        }
                    }
                    if (transFlag) {
                        //把赠品写入ERP单身
                        Integer seq = 9999;
                        for (Dto gwcsz_tPO : gwcsz_tPOs) {
                            //单身信息
                            Dto qDto = Dtos.newDto();

                            qDto.put("ERP_Link", dDto.getString("ERP_Link"));//目标数据库
                            qDto.put("MB003", dbname);//目标数据库
                            //按法人组织抓取单别
                            Dto dhinfo = gwcsTService.getGwaz(shiroUser.getDBC(), gwcs_tPOs.get(0).getString("GWCSMLEGAL"));
                            qDto.put("TD001", dhinfo.getString("GWAZ002"));
                            qDto.put("TD002", dd01);
                            DecimalFormat mFormat = new DecimalFormat("0000");//确定格式，把1 转换为0001
                            String gwcsseq = mFormat.format(seq);
                            qDto.put("TD003", gwcsseq);
                            qDto.put("TD004", gwcsz_tPO.getString("GWCSZ004"));//取CRM中订单赠品的品号(需要判断是否在ERP中是否存在?）
                            qDto.put("TD005", gwcsz_tPO.getString("GWAML003"));
                            qDto.put("TD006", gwcsz_tPO.getString("GWAML004"));
                            qDto.put("TD007", gwcs_tPOs.get(0).getString("GWCS113"));
                            qDto.put("TD008", gwcsz_tPO.getString("GWCSZ005"));
                            qDto.put("TD009", 0);
                            //qDto.put("TD010", gwcs_tPOs.get(0).getString("GWCS002")); //单位
                            qDto.put("TD010", gwcs_tPOs.get(0).getString("GWABL003")); //单位
                            qDto.put("TD011", 0);//单价
                            qDto.put("TD012", 0);
                            if (gwcs_tPOs.get(0).getDate("GWCS016") != null) {
                                qDto.put("TD013", DateUtils.formatDate(gwcs_tPOs.get(0).getDate("GWCS016"), "yyyyMMdd"));
                            }
                            qDto.put("TD016", "N");
                            qDto.put("TD020", gwcs_tPOs.get(0).getString("GWDC003"));
                            qDto.put("TD021", "Y");
                            qDto.put("TD026", 1);
                            //qDto.put("TD036", gwcs_tPO.getString("GWCS002"));
                            Dto mbDto = Dtos.newDto();
                            mbDto.put("MB003", dbname);
                            mbDto.put("ERP_Link", dDto.getString("ERP_Link"));
                            mbDto.put("MB001", gwcsz_tPO.getString("GWCSZ004"));
                            Dto mb = this.baseMapper.getMB(mbDto);
                            if (mb != null) {
                                qDto.put("TD036", mb.getString("MB090"));//包装单位
                                mbDto.put("TD036", mb.getString("MB090"));
                                Dto tDto = this.baseMapper.getTD032(mbDto);
                                if (mb != null && tDto != null) {
                                    //qDto.put("TD032",gwcsz_tPO.getBigDecimal("GWCSZ005").multiply(tDto.getBigDecimal("TD032")));//包装数量
                                    qDto.put("TD032", tDto.getBigDecimal("TD032"));//包装数量
                                } else {
                                    qDto.put("TD032", 0);
                                }
                            } else {
                                qDto.put("TD032", 0);
                            }

                            qDto.put("TD048", gwcsseq);
                            qDto.put("TD049", gwcs_tPOs.get(0).getString("GWCSDOCNO"));
                            //qDto.put("TD050", 0);
                            qDto.put("TD062", "N");
                            //qDto.put("TD200", 0);
                            qDto.put("TDD01", "N");

                            qDto.put("UDF07", qDto.getString("TD004"));
                            qDto.put("UDF61", 0);
                            //qDto.put("UDF62", 0);
                            qDto.put("COMPANY", gwcs_tPOs.get(0).getString("GWCSPLANT"));
                            //qDto.put("CREATOR", shiroUser.getBIZ_CODE());
                            //qDto.put("USR_GROUP", "");
                            //qDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMdd"));
                            qDto.put("FLAG", 1);

                            inDto.put("gwcs102", count);
                            count++;
                            try {
                                this.baseMapper.insertCOPTD(qDto);
                            } catch (Exception e) {
                                transFlag = false;
                                this.baseMapper.deleteCOPTD(qDto);
                                JSONObject jo = new JSONObject();
                                jo.put("ERPRESULT", "N");//同步结果  Y  N
                                jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                                jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                                jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                                jo.put("GWCS045", "");//订单号
                                jo.put("REMARK", "insertCOPTD：" + e.getMessage());
                                json.add(jo);
                                //插入COPTD 失败  整张订单跳过 进行下个订单
                                break;
                            }

                            inDto.put("gwcszdocno", gwcsz_tPO.getString("GWCSZDOCNO"));
                            inDto.put("gwcszent", gwcsz_tPO.getString("GWCSZENT"));
                            inDto.put("gwcsz003", gwcsz_tPO.getString("GWCSZ003"));
                            inDto.put("gwcszseq", gwcsseq);
                            seq--;
                            try {
                                this.gwcsTService.updateGWCSZ(inDto);
                            } catch (Exception e) {
                                transFlag = false;
                                JSONObject jo = new JSONObject();
                                jo.put("ERPRESULT", "N");//同步结果  Y  N
                                jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                                jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                                jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                                jo.put("GWCS045", "");//订单号
                                jo.put("REMARK", "updateGWCSZ：" + e.getMessage());
                                json.add(jo);
                                //插入COPTD 失败  整张订单跳过 进行下个订单
                                break;
                            }

                        }
                    }

                } catch (Exception e) {
                    transFlag = false;
                    JSONObject jo = new JSONObject();
                    jo.put("ERPRESULT", "N");//同步结果  Y  N
                    jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                    jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                    jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                    jo.put("GWCS045", "");//订单号
                    jo.put("REMARK", "失败：" + e.getMessage());
                    json.add(jo);
                    //插入COPTD 失败  整张订单跳过 进行下个订单
                    continue;
                }

                //单头  订单信息
                Dto pDto = Dtos.newDto();

                pDto.put("ERP_Link",dDto.getString("ERP_Link"));
                pDto.put("MB003", dbname);//目标数据库
                //按法人组织抓取单号
                Dto dhinfo = gwcsTService.getGwaz(shiroUser.getDBC(), gwcs_tPOs.get(0).getString("GWCSMLEGAL"));
                pDto.put("TC001", dhinfo.getString("GWAZ002"));
                pDto.put("TC002", dd01);
                pDto.put("TC003", DateUtils.getDate("yyyyMMdd"));
                pDto.put("TC004", gwcs_tPOs.get(0).getString("GWCS010"));
                //pDto.put("TC005", gwcs_tPOs.get(0).getString("ERP_DEPT"));//gwcs014归属的部门的ERP组织？biz_code_
                //pDto.put("TC006", gwcs_tPOs.get(0).getString("GWCS014_BIZCODE"));
                inDto.put("GWCS010", gwcs_tPOs.get(0).getString("GWCS010"));
                Dto yDto = this.baseMapper.getYewu(pDto);
                if (yDto != null) {
                    pDto.put("TC005", yDto.getString("MA015"));
                    //pDto.put("TC006", yDto.getString("BIZ_CODE"));
                }
                if (adrDto != null) {
                    //pDto.put("TC006", adrDto.getString("GWAL021"));
                    pDto.put("TC018", adrDto.getString("GWAL021"));
                    inDto.put("flag", true);//抓取送货地址
                    Dto address = this.gwcsTService.getAddress(inDto);
                    if (address != null) {
                        String address_ = address.getString("NAME010") + address.getString("NAME011")
                                + address.getString("NAME012") + address.getString("NAME013")
                                + address.getString("NAME014") + address.getString("NAME015")
                                + address.getString("NAME016") + address.getString("NAME017")
                                + address.getString("GWAL018");
                        pDto.put("TC010", address_);
                    }
                }
                pDto.put("TC007", "01");
                pDto.put("TC008", gwcs_tPOs.get(0).getString("GWCS006"));
                if (gwcs_tPOs.get(0).getBigDecimal("GWCS034") == null) {
                    gwcs_tPOs.get(0).put("GWCS034", 1);//汇率没有默认给1
                }
                pDto.put("TC009", gwcs_tPOs.get(0).getBigDecimal("GWCS034"));
                //pDto.put("TC011", gwcs_tPOs.get(0).getString("GWCS008") + gwcs_tPOs.get(0).getString("GWCS009"));
                pDto.put("TC014", gwakDto.getString("GWAK060_NAME")); //付款条件说明
                pDto.put("TC015", gwcs_tPOs.get(0).getString("GWCS050"));
                pDto.put("TC016", "1");

                //pDto.put("TC019", "1");
                pDto.put("TC026", 0);
                pDto.put("TC027", "Y");
                pDto.put("TC028", 0);
                //几个需要求遍历订单求和的数据
                BigDecimal sum_TC029 = new BigDecimal(0);
                BigDecimal sum_TC030 = new BigDecimal(0);
                BigDecimal sum_TC031 = new BigDecimal(0);
                BigDecimal sum_TCI03 = new BigDecimal(0);
                BigDecimal sum_TCI04 = new BigDecimal(0);
                for (Dto gwcs_tPO : gwcs_tPOs) {
                    if (gwcs_tPO.getBigDecimal("TD038") != null) {
                        sum_TC029 = sum_TC029.add(gwcs_tPO.getBigDecimal("GWCS005").subtract(gwcs_tPO.getBigDecimal("GWCS040")));
                        sum_TC029 = sum_TC029.setScale(2, RoundingMode.HALF_UP);
                    }
                    if (gwcs_tPO.getBigDecimal("GWCS040") != null) {
                        sum_TC030 = sum_TC030.add(gwcs_tPO.getBigDecimal("GWCS040"));
                        sum_TC030 = sum_TC030.setScale(2, RoundingMode.HALF_UP);
                    }
                    if (gwcs_tPO.getBigDecimal("GWCS003") != null) {
                        sum_TC031 = sum_TC031.add(gwcs_tPO.getBigDecimal("GWCS003"));
                        sum_TC031 = sum_TC031.setScale(2, RoundingMode.HALF_UP);
                    }
                    if (gwcs_tPO.getBigDecimal("TDI01") != null) {
                        sum_TCI03 = sum_TCI03.add(gwcs_tPO.getBigDecimal("TDI01"));
                        sum_TCI03 = sum_TCI03.setScale(2, RoundingMode.HALF_UP);
                    }
                    if (gwcs_tPO.getBigDecimal("TDI04") != null) {
                        sum_TCI04 = sum_TCI04.add(gwcs_tPO.getBigDecimal("TDI04"));
                        sum_TCI04 = sum_TCI04.setScale(2, RoundingMode.HALF_UP);
                    }
                }
                pDto.put("TC029", sum_TC029);//sum（GWCS005）
                pDto.put("TC030", sum_TC030);//sum（gwcs040）
                pDto.put("TC031", sum_TC031);//sum（gwcs003）
                pDto.put("TC032", gwcs_tPOs.get(0).getString("GWCS010"));
                pDto.put("TC039", DateUtils.getDate("yyyyMMdd"));
                pDto.put("TC040", gwcs_tPOs.get(0).getString("GWCS014_BIZCODE"));
                if (gwcs_tPOs.get(0).getBigDecimal("GWEA006") == null || gwcs_tPOs.get(0).getBigDecimal("GWEA006").equals(new BigDecimal(0.00))) {
                    pDto.put("TC041", 0.16);
                } else {
                    pDto.put("TC041", gwcs_tPOs.get(0).getBigDecimal("GWEA006").divide(new BigDecimal(100), 4, RoundingMode.HALF_UP));//根据税种gwcs032抓税率
                }
                pDto.put("TC042", gwakDto.getString("GWAK060"));//客户资料对应付款编号
                pDto.put("TC043", 0);
                pDto.put("TC044", 0);
                pDto.put("TC045", 0);
                pDto.put("TC046", 0);
                pDto.put("TC048", "N");
                pDto.put("TC050", "N");
                pDto.put("TC058", 0);
                pDto.put("TC059", "N");
                pDto.put("TC068", 0);
                pDto.put("TC069", 0);
                pDto.put("TC070", "N");
                pDto.put("TC071", "0");
                pDto.put("TC073", gwcs_tPOs.get(0).getString("GWAK007"));//按经销商gwcs011去客户档gwak抓税号gwak007
                if (adrDto != null) {
                    pDto.put("TC074", adrDto.getString("GWAL020"));
                    pDto.put("TC075", adrDto.getString("GWAL020"));//传真
                }
                //pDto.put("TC200", "");//分销单头返利类型:无返利=2，有返利=1，铺货=2
                //if (gwcs_tPOs.get(0).getString("GWCS117").equals("1") || gwcs_tPOs.get(0).getString("GWCS117").equals("3")) {
                //    pDto.put("TC200", "1");
                //}
                //if (gwcs_tPOs.get(0).getString("GWCS117").equals("2") || gwcs_tPOs.get(0).getString("GWCS117").equals("4")) {
                //    pDto.put("TC200", "2");
                //}
                //if (gwcs_tPOs.get(0).getString("GWCS117").equals("5")) {
                //    pDto.put("TC200", "2");
                //}
                pDto.put("TC201", "N");
                pDto.put("TC202", "N");
                if (gwcs_tPOs.get(0).getDate("GWCS016") != null) {
                    pDto.put("TCI01", DateFormatUtils.format(gwcs_tPOs.get(0).getDate("GWCS016"), "yyyyMMdd"));
                    pDto.put("TCI02", DateFormatUtils.format(gwcs_tPOs.get(0).getDate("GWCS016"), "yyyyMMdd"));
                }

                pDto.put("TCI03", sum_TCI03);//sum(gwcs005*gwcs034)
                pDto.put("TCI04", sum_TCI04);//sum(gwcs040*gwcs034)
                pDto.put("UDF03", "正式");
                pDto.put("TC065", gwcs_tPOs.get(0).getString("GWCSDOCNO"));
                pDto.put("UDF12", gwcs_tPOs.get(0).getString("GWCS117"));
                switch (gwcs_tPOs.get(0).getString("GWCS117")) {
                    case "1":
                        pDto.put("UDF12", "现款有返利");
                        break;
                    case "2":
                        pDto.put("UDF12", "现款无返利");
                        break;
                    case "3":
                        pDto.put("UDF12", "帐期有返利");
                        break;
                    case "4":
                        pDto.put("UDF12", "帐期无返利");
                        break;
                    case "5":
                        pDto.put("UDF12", "铺货有返利");
                        break;
                    case "6":
                        pDto.put("UDF12", "铺货无返利");
                        break;
                }
                pDto.put("COMPANY", gwcs_tPOs.get(0).getString("GWCSPLANT"));
                //pDto.put("CREATOR", shiroUser.getBIZ_CODE());
                pDto.put("USR_GROUP", "");
                //pDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMdd"));
                pDto.put("FLAG", 1);

                inDto.put("gwcs045", dd01);

                int row = 0;
                if (transFlag) {
                    try {
                        row = this.baseMapper.insertCOPTC(pDto);
                    } catch (Exception e) {
                        JSONObject jo = new JSONObject();
                        jo.put("ERPRESULT", "N");//同步结果  Y  N
                        jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                        jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                        jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                        jo.put("GWCS045", "");//订单号
                        jo.put("REMARK", "insertCOPTC：" + e.getMessage());
                        json.add(jo);
                        continue;
                    }
                }

                String result = "N";
                if (row > 0) {
                    result = "Y";
                }
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", result);//同步结果  Y  N
                jo.put("GWAK001", gwcs_tPOs.get(0).getString("GWAK001"));//客户编号
                jo.put("GWAKL003", gwcs_tPOs.get(0).getString("GWAKL003_KH"));//客户名称
                jo.put("GWCSDOCNO", gwcs_tPOs.get(0).getString("GWCSDOCNO"));//订单号
                if (result == "Y") {
                    jo.put("GWCS045", dd01);//ERP订单号
                    jo.put("REMARK", "");
                    json.add(jo);
                }
                /*if(result=="N"){
                    jo.put("REMARK","插入COPTC失败");
                }else {
                    jo.put("REMARK","");
                }*/
            }
            rows++;
        }
        return json;
    }

    /**
     * 客户资料同步
     */
    public String SyncERP(String gwak001, String ERP_Link,String MB003) {
        ShiroUser shiroUser = ShiroKit.getUser();

        //获取gwak001对应的客户资料数据
        Dto qDto = Dtos.newDto();
        qDto.put("ent", shiroUser.getDBC());
        qDto.put("gwak001", gwak001);
        Dto info = this.gwakTService.getGwakData(qDto);

        //获取目标数据库
        //Dto dbinfo = this.gwakTService.getDBinfo(MB001);
        Dto outDto = Dtos.newDto();
        //outDto.put("MB001", dbinfo.getString("MB001").trim());//获取目标数据库
        outDto.put("ERP_Link",ERP_Link);
        outDto.put("MB003", MB003);
        outDto.put("MA001", info.getString("GWAK001"));
        outDto.put("MA002", info.getString("GWAKL003"));
        outDto.put("MA003", info.getString("GWAKL004"));
        outDto.put("MA004", info.getString("GWAK050"));
        outDto.put("MA005", info.getString("GWAK031"));//联系人
        outDto.put("MA006", info.getString("GWAK030"));//电话1
        //outDto.put("MA007", info.getString("GWAK050"));//电话2
        //outDto.put("MA010", info.getString("GWAK007"));
        if (info.getString("GWAK051").equals("NULL")) {
            outDto.put("MA011", "");
        } else {
            outDto.put("MA011", info.getBigDecimal("GWAK051"));
        }
        if (info.getString("GWAK052").equals("NULL")) {
            outDto.put("MA012", "");
        } else {
            outDto.put("MA012", info.getBigDecimal("GWAK052"));
        }
        outDto.put("MA014", info.getString("GWAK008"));
        outDto.put("MA017", info.getString("GWAK023"));
        outDto.put("MA018", info.getString("GWAK024"));
        outDto.put("MA019", info.getString("GWAK021"));
        outDto.put("MA023", info.getString("GWAK028"));//注册地址
        if (!(info.getString("GWAL018_fp").isEmpty())) {
            outDto.put("MA025", info.getString("GWAL018_fp"));//gwal018 where gwal003='I' 发票地址
        }
        if (!(info.getString("GWAL018_sh").isEmpty())) {
            outDto.put("MA027", info.getString("GWAL018_sh"));//gwal018 where gwal003='D' 送货地址
        }
        outDto.put("MA031", info.getString("GWAK060_NAME"));
        outDto.put("MA032", "N");
        outDto.put("MA033", 0);
        outDto.put("MA034", 0);
        outDto.put("MA035", "2");
        outDto.put("MA036", 0);
        outDto.put("MA037", "A");
        //outDto.put("MA038", info.getString("GWAK012"));//税种
        outDto.put("MA038", "1");//税种
        outDto.put("MA039", "1");
        outDto.put("MA041", "2");
        outDto.put("MA042", "1");
        outDto.put("MA044", 0);
        outDto.put("MA045", 0);
        outDto.put("MA047", "112201"); //账款科目
        //outDto.put("MA048", "1");
        outDto.put("MA059", 0);
        outDto.put("MA060", 0);
        outDto.put("MA061", 0);
        outDto.put("MA062", "");//地址
        outDto.put("MA066", "N");
        outDto.put("MA067", 0);
        outDto.put("MA074", "1121");
        outDto.put("MA075", "86");
        outDto.put("MA082", "N");
        outDto.put("MA083", info.getString("GWAK060"));//付款条件
        outDto.put("MA084", "N");
        outDto.put("MA086", "N");
        outDto.put("MA087", "2");
        outDto.put("MA088", "1");
        outDto.put("MA089", "1");
        outDto.put("MA090", 1);
        outDto.put("MA091", 1);
        outDto.put("MA092", 1);
        outDto.put("MA093", 1);
        outDto.put("MA094", 1);
        outDto.put("MA095", 0);
        outDto.put("MA096", "1");
        outDto.put("MA097", "1");
        //抓税率
        if (info.getString("GWEA006") == null || info.getString("GWAK004").isEmpty()) {
            outDto.put("MA101", 0.16);
        } else {
            outDto.put("MA101", info.getBigDecimal("GWEA006"));
        }
        Dto orgDto = this.gwcsTService.getSysOrg(shiroUser.getORG_ID());
        outDto.put("MA103", "1");
        outDto.put("MA104", 0);
        outDto.put("MA105", "220301");
        outDto.put("MA106", "N");
        outDto.put("MA109", 0);
        outDto.put("MA110", 0);
        outDto.put("MA111", 0);
        outDto.put("MA113", "N");
        //outDto.put("UDF01", info.getString("GWAK004"));
        outDto.put("COMPANY", info.getString("GWAKPLANT"));

        if (orgDto != null) {
            //outDto.put("COMPANY", orgDto.getString("BIZ_CODE"));
            //outDto.put("USR_GROUP", orgDto.getString("BIZ_CODE"));
        }
        //outDto.put("CREATOR", shiroUser.getBIZ_CODE());
        //outDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMddSSSSS"));
        outDto.put("FLAG", 1);

        //暂时直接写入Y
        //String result = "Y";
        String result = "N";
        //将客户资料插入指定ERP数据库
        int i = this.baseMapper.insertCOPMA(outDto);
        if (i > 0) {
            result = "Y";
        }
        return result;
    }

    /**
     * 还原ERP订单
     * @param qDto
     * @return
     */
    public Dto restore710(Dto qDto){
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = qDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);
        int rows = 0;
        String err_message = "";
        for (String data : rid) {
            Dto inDto = qDto;
            inDto.put("GWCSDOCNO", data);
            List<Dto> gwcs = this.gwcsTService.getGWCS045(inDto);
            Boolean flag1 = this.gwcsTService.getGwdfCount(inDto);
            Boolean flag2 = false;
            //根据订单上的法人组织字段gwcsmlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值。
            Dto dbinfo1 = this.gwcsTService.getDBinfo1(shiroUser.getDBC(), gwcs.get(0).getString("GWCSMLEGAL"));// 先查询biz_code_  因数据库排序问题  分成两个语句查询
            dbinfo1.put("ERP_Link",qDto.getString("ERP_Link"));
            dbinfo1.put("ERP_DB",qDto.getString("ERP_DB"));
            Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);
            String dbname = dbinfo2.getString("MB003").trim();

            for (Dto tDto : gwcs) {
                tDto.put("ERP_Link",qDto.getString("ERP_Link"));
                tDto.put("MB003",dbname);
                Dto result = this.baseMapper.getCopthCount(tDto);
                if (result.getBigDecimal("COUNT").compareTo(new BigDecimal(0)) == 1) {
                    flag2 = true;
                    break;
                }
            }
            if (flag1 || flag2) {
                err_message = data + ',' + err_message;
            }
        }
        err_message = StringUtils.substringBeforeLast(err_message, ",");
        if (!("").equals(err_message)) {
            outDto.setAppCode(300);
            outDto.put("docno","订单" + err_message + "已做过出货单或变更单，无法撤销！");
        }
        for (String data : rid) {
            Dto inDto = Dtos.newDto();
            inDto.put("gwcsdocno", data);
            inDto.put("ent", shiroUser.getDBC());
            inDto.put("TC065", data);
            List<Dto> pList = gwcsTService.listGwcs032(inDto);
            for (int t = 0; t < pList.size(); t++) {
                List<Dto> gwcs_tPOs = this.gwcsTService.listGwcsInfo(inDto);

                //根据订单上的法人组织字段gwcsmlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值。
                Dto dbinfo1 = this.gwcsTService.getDBinfo1(shiroUser.getDBC(), gwcs_tPOs.get(0).getString("GWCSMLEGAL"));// 先查询biz_code_  因数据库排序问题  分成两个语句查询
                dbinfo1.put("ERP_Link",qDto.getString("ERP_Link"));
                dbinfo1.put("ERP_DB",qDto.getString("ERP_DB"));
                Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);
                String dbname = dbinfo2.getString("MB003").trim();

                inDto.put("TC027", "V");
                inDto.put("MB003", dbname);
                inDto.put("ERP_Link", qDto.getString("ERP_Link"));
                Dto COPTCinfo = this.baseMapper.getCOPTC(inDto);
                if (COPTCinfo == null) {
                    GwcsT gwcsT = new GwcsT();
                    gwcsT.setGWCSACTI("Y");
                    gwcsT.setGWCSENT(shiroUser.getDBC());
                    gwcsT.setGWCSDOCNO(gwcs_tPOs.get(0).getString("GWCSDOCNO"));
                    gwcsT.setGWCS045("  ");
                    gwcsTService.updateGwcs(gwcsT);
                    outDto.setAppCode(500);
                    outDto.put("docno", "找不到对应ERP订单");
                } else {
                    Dto inDto2 = Dtos.newDto();
                    inDto2.put("MB003", dbname);
                    inDto2.put("TC027", "V");
                    inDto2.put("TC002", COPTCinfo.getString("TC002"));
                    inDto2.put("TD002", COPTCinfo.getString("TC002"));
                    inDto2.put("TD021", "V");
                    inDto2.put("TC065", data);
                    inDto2.put("ERP_Link", qDto.getString("ERP_Link"));
                    baseMapper.updateCOPTC(inDto2);

                    GwcsT gwcsT = new GwcsT();
                    gwcsT.setGWCSACTI("Y");
                    gwcsT.setGWCSENT(shiroUser.getDBC());
                    gwcsT.setGWCSDOCNO(gwcs_tPOs.get(0).getString("GWCSDOCNO"));
                    //gwcsT.setGWCS045(COPTCinfo.getString("TC002"));
                    gwcsT.setGWCS045("  ");
                    gwcsTService.updateGwcs(gwcsT);
                    outDto.setAppCode(200);
                    outDto.put("docno", "操作成功");
                } /*else if (COPTCinfo.getString("TC027").equals("Y")) {
                    outDto.put("docno", "ERP订单已经审核不能还原");
                }*/
            }
            rows++;
        }
        return outDto;
    }

    /**
     * 维修费用结算转订单(gwei024)
     */
    public JSONArray transfer024(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = inDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        JSONArray json = new JSONArray();
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);

        for (String data : rid) {
            inDto.put("MMRWDOCNO", data);
            //根据单号查询维修费用结算表数据
            List<Dto> mmrwData = this.mmrwTService.getMmrwAllData(inDto);

            //根据订单上的法人组织字段mmrwlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值
            Dto dbinfo1 = this.mmrwTService.getDBinfo1(mmrwData.get(0).getString("MMRWLEGAL"));// 先查询biz_code_  因数据库排序问题  分成两个语句查询
            dbinfo1.put("ERP_Link",inDto.getString("ERP_Link"));
            Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);

            if (dbinfo2 == null) { //没查到目标数据库  添加失败
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("MMRWDOCNO", mmrwData.get(0).getString("MMRWDOCNO"));//维修费用结算单号
                jo.put("ERPDOCNO", "");//erp单号
                jo.put("REASON", "目标数据库为空,添加失败");//失败原因
                json.add(jo);
                continue;
            }

            String dbname = dbinfo2.getString("MB003").trim();

            String docno = DateUtils.getDate("yyyyMMddSSS");
            String TB001 = "";
            try {
                int count = 1;
                for (Dto mmrw : mmrwData) {
                    //单身信息
                    Dto qDto = Dtos.newDto();
                    qDto.put("ERP_Link", inDto.getString("ERP_Link"));
                    qDto.put("MB001", dbname);//目标数据库
                    //按法人组织抓取单号
                    Dto dhinfo = this.mmrwTService.getGwaz(mmrw.getString("MMRWLEGAL"));
                    qDto.put("TB001", dhinfo.getString("GWAZ002"));
                    TB001 = dhinfo.getString("GWAZ002");
                    qDto.put("TB002", docno);
                    qDto.put("TB003", BigDecimal.valueOf(count));
                    qDto.put("TB004", mmrw.getString("MMRW001"));//品号
                    Dto tDto = Dtos.newDto();
                    tDto.put("GWEB002", mmrw.getString("MMRA001")); //客户编号
                    tDto.put("GWEB004", mmrw.getString("MMRW001"));
                    tDto.put("ENT", shiroUser.getDBC());
                    Dto result = this.mmrwTService.getGwebData1(tDto);
                    if (result.size() == 0) {
                        result = this.mmrwTService.getGwebData2(tDto);
                    }
                    qDto.put("TB005", result.getString("GWEB008"));//品名
                    qDto.put("TB006", result.getString("GWEB009"));//规格
                    qDto.put("TB007", mmrw.getBigDecimal("MMRW003"));//数量
                    qDto.put("TB008", mmrw.getString("MMRW002"));//单位
                    qDto.put("TB009", "");//库存数量
                    qDto.put("TB010", "");//单位成本
                    qDto.put("TB011", "");//金额
                    qDto.put("TB012", mmrw.getString("MMRW005"));//转出库
                    qDto.put("TB013", "");//转入库
                    qDto.put("TB014", "********************");//批号
                    qDto.put("TB015", "");//有效日期
                    qDto.put("TB016", "");//附件日期
                    qDto.put("TB017", "");//备注
                    qDto.put("TB018", "N");//审核码
                    qDto.put("TB019", "");//预留字段
                    qDto.put("TB020", "");//小单位
                    qDto.put("TB021", "");//项目编号
                    qDto.put("TB022", "");//包装数量
                    qDto.put("TB023", "");//包装单位
                    qDto.put("TB024", "");//存储位置
                    qDto.put("TB025", "");//已销毁数量
                    qDto.put("TB026", "");//件装
                    qDto.put("TB027", "");//件数
                    qDto.put("TB028", "");//批号说明
                    qDto.put("TB029", "##########");//转出库位
                    qDto.put("TB030", "");//转入库位
                    qDto.put("TB031", "");//生产日期
                    qDto.put("TB032", "");
                    qDto.put("TB033", "");
                    qDto.put("TB034", "");
                    qDto.put("TB035", "");
                    qDto.put("TB036", "");
                    qDto.put("TB037", "");
                    qDto.put("UDF03", mmrw.getString("MMRWDOCNO"));//费用结算单单号

                    this.baseMapper.insert_INVTB(qDto);

                    MmrwT mmrwT = new MmrwT();
                    mmrwT.setMMRWACTI("Z");
                    mmrwT.setMMRW013(docno);
                    mmrwT.setMMRW014(String.valueOf(count));
                    UpdateWrapper mmrwTUpdateWrapper = new UpdateWrapper();
                    mmrwTUpdateWrapper.eq("MMRWSEQ",mmrw.getBigDecimal("MMRWSEQ"));
                    mmrwTUpdateWrapper.eq("MMRWSENT",shiroUser.getDBC());
                    mmrwTUpdateWrapper.eq("MMRWDOCNO",data);
                    mmrwT.update(mmrwTUpdateWrapper);
                    count++;
                }

            } catch (Exception e) {
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("MMRWDOCNO", mmrwData.get(0).getString("MMRWDOCNO"));//维修费用结算单号
                jo.put("ERPDOCNO", "");//erp单号
                jo.put("REASON", "添加INVTB表失败");//失败原因

                e.printStackTrace();

                Dto deltb = Dtos.newDto();
                deltb.put("MB001", dbname);
                deltb.put("TB001", TB001);
                deltb.put("TB002",docno);
                deltb.put("ERP_Link", inDto.getString("ERP_Link"));
                this.baseMapper.delete_INVTB(deltb);

                MmrwT mmrwT = new MmrwT();
                mmrwT.setMMRWACTI("Y");
                mmrwT.setMMRW013("");
                mmrwT.setMMRW014("");
                UpdateWrapper mmrwTUpdateWrapper = new UpdateWrapper();
                mmrwTUpdateWrapper.eq("MMRWSENT",shiroUser.getDBC());
                mmrwTUpdateWrapper.eq("MMRWDOCNO",data);
                mmrwT.update(mmrwTUpdateWrapper);

                json.add(jo);
                continue;
            }

            try {
                //单头  订单信息
                Dto mmrwDto = mmrwData.get(0);
                Dto pDto = Dtos.newDto();

                pDto.put("ERP_Link", inDto.getString("ERP_Link"));
                pDto.put("MB001", dbname);//目标数据库
                //按法人组织抓取单号
                Dto dhinfo = this.mmrwTService.getGwaz(mmrwDto.getString("MMRWLEGAL"));
                pDto.put("TA001", dhinfo.getString("GWAZ002"));
                pDto.put("TA002", docno);
                pDto.put("TA003", mmrwDto.getString("MMRWDOCDATE").replace("-", ""));//交易日期
                pDto.put("TA004", mmrwDto.getString("MMRW017_BIZ_CODE"));//部门编号 mmrw017对应的业务对照码
                pDto.put("TA005", mmrwDto.getString("MMRW015"));//备注
                pDto.put("TA006", "N"); //审核码
                pDto.put("TA007", "0"); //打印次数
                pDto.put("TA008", "01"); //工厂编号
                pDto.put("TA009", "11");  //单据性质码
                pDto.put("TA010", 0);  //件数
                pDto.put("TA011", 0);  //总数量
                pDto.put("TA012", 0);  //总金额
                pDto.put("TA013", "N"); //生成分录
                pDto.put("TA014", DateUtils.getDate("yyyyMMdd")); //单据日期
                pDto.put("TA015", ""); //审核者
                pDto.put("TA016", 0); //总包装数量
                pDto.put("TA017", "N"); //签核状态码
                pDto.put("TA018", ""); //海关手册
                pDto.put("TA019", ""); //审批意见
                pDto.put("TA020", ""); //不合格原因
                pDto.put("TA021", ""); //传送次数
                pDto.put("TA022", ""); //汇入日期
                pDto.put("TA023", ""); //门店编号
                pDto.put("TA024", ""); //对方汇出日期
                pDto.put("TA025", "1"); //单据来源
                pDto.put("TA026", "");
                pDto.put("TA027", "");
                pDto.put("TA028", "");
                pDto.put("TA029", "");
                pDto.put("TA030", "");
                pDto.put("TA031", "");
                pDto.put("TA032", "");// 人员编号 用户对应的业务对照码
                pDto.put("TA033", "");// 项目编号
                pDto.put("UDF01", "山东售后仓");
                pDto.put("UDF02", "领料");

                int row = this.baseMapper.insert_INVTA(pDto);

                String result = "N";
                if (row > 0) {
                    result = "Y";
                }

                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", result);//同步结果  Y  N
                jo.put("MMRWDOCNO", mmrwData.get(0).getString("MMRWDOCNO"));//维修费用结算单号
                jo.put("ERPDOCNO", docno);//erp单号
                jo.put("REASON", "");//失败原因
                json.add(jo);
            } catch (Exception e) {
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("MMRWDOCNO", mmrwData.get(0).getString("MMRWDOCNO"));//维修费用结算单号
                jo.put("ERPDOCNO", docno);//erp单号
                jo.put("REASON", "添加INVTA表失败");//失败原因
                json.add(jo);

                e.printStackTrace();

                Dto deltb = Dtos.newDto();
                deltb.put("MB001", dbname);
                deltb.put("TB001", TB001);
                deltb.put("TB002", docno);
                deltb.put("ERP_Link", inDto.getString("ERP_Link"));
                this.baseMapper.delete_INVTB(deltb);

                MmrwT mmrwT = new MmrwT();
                mmrwT.setMMRWACTI("Y");
                mmrwT.setMMRW013("");
                mmrwT.setMMRW014("");
                UpdateWrapper mmrwTUpdateWrapper = new UpdateWrapper();
                mmrwTUpdateWrapper.eq("MMRWSENT",shiroUser.getDBC());
                mmrwTUpdateWrapper.eq("MMRWDOCNO",data);
                mmrwT.update(mmrwTUpdateWrapper);

                //插入INVTA 失败  整张订单跳过 进行下个订单
                e.printStackTrace();
                continue;
            }
        }
        return json;
    }

    /**
     * 还原订单(gwei024)
     */
    public Dto restore024(Dto inDto) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = inDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);
        int rows = 0;
        for (String data : rid) {
            inDto.put("MMRWDOCNO", data);

            //根据单号查询维修费用结算表数据
            List<Dto> mmrwData = this.mmrwTService.getMmrwAllData(inDto);
            //根据订单上的法人组织字段mmrwlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值
            Dto dbinfo1 = this.mmrwTService.getDBinfo1(mmrwData.get(0).getString("MMRWLEGAL"));// 先查询biz_code_  因数据库排序问题  分成两个语句查询
            dbinfo1.put("ERP_Link",inDto.getString("ERP_Link"));
            Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);
            String dbname = dbinfo2.getString("MB003").trim();

            inDto.put("MB001", dbname);
            inDto.put("MMRW013", mmrwData.get(0).getString("MMRW013"));
            Dto COPTAinfo = this.baseMapper.getINVTA(inDto);
            if (COPTAinfo == null) {
                MmrwT mmrwT = new MmrwT();
                mmrwT.setMMRWACTI("Y");
                mmrwT.setMMRW013("");
                mmrwT.setMMRW014("");
                UpdateWrapper mmrwTUpdateWrapper = new UpdateWrapper();
                mmrwTUpdateWrapper.eq("MMRWSENT",shiroUser.getDBC());
                mmrwTUpdateWrapper.eq("MMRWDOCNO",data);
                mmrwT.update(mmrwTUpdateWrapper);
                outDto.put("docno", "找不到对应ERP订单");
            } else if (COPTAinfo.getString("TA006").equals("N")) {
                Dto inDto2 = Dtos.newDto();
                inDto2.put("ERP_Link", inDto.getString("ERP_Link"));
                inDto2.put("MB001", dbname);
                inDto2.put("TA006", "V");
                inDto2.put("TA002", COPTAinfo.getString("TA002"));
                this.baseMapper.updateINVTA(inDto2);

                MmrwT mmrwT = new MmrwT();
                mmrwT.setMMRWACTI("Y");
                mmrwT.setMMRW013("");
                mmrwT.setMMRW014("");
                UpdateWrapper mmrwTUpdateWrapper = new UpdateWrapper();
                mmrwTUpdateWrapper.eq("MMRWSENT",shiroUser.getDBC());
                mmrwTUpdateWrapper.eq("MMRWDOCNO",data);
                mmrwT.update(mmrwTUpdateWrapper);
                outDto.put("docno", "操作成功");
            } else if (COPTAinfo.getString("TA006").equals("Y")) {
                outDto.put("docno", "ERP单据已经审核不能还原");
            }
            rows++;
        }
        return outDto;
    }

    /**
     * 维修费用收入转订单(gwei025)
     */
    public JSONArray transfer025(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = inDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        JSONArray json = new JSONArray();
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);

        for (String data : rid) {
            inDto.put("MMRXDOCNO", data);
            //根据单号查询维修收入结算表数据
            List<Dto> mmrx_tPOs = this.mmrxTService.getMmrxAllData(inDto);

            //根据订单上的法人组织字段mmrwlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值
            Dto dbinfo1 = Dtos.newDto();
            dbinfo1.put("BIZ_CODE",mmrx_tPOs.get(0).getString("MMRXPLANT"));
            dbinfo1.put("ERP_Link",inDto.getString("ERP_Link"));
            dbinfo1.put("ERP_DB",inDto.getString("ERP_DB"));
            Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);

            if (dbinfo2 == null) { //没查到目标数据库  添加失败
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));//客户编号
                jo.put("GWAKL003",  mmrx_tPOs.get(0).getString("MMRX018_NAME"));//客户名称
                jo.put("MMRXDOCNO",  mmrx_tPOs.get(0).getString("MMRXDOCNO"));//订单号
                json.add(jo);
                continue;
            }

            String dbname = dbinfo2.getString("MB003").trim();
            String ERP_Link = inDto.getString("ERP_Link");

            String dd01;
            Date date = new Date();
            Dto tcDto = Dtos.newDto();
            tcDto.put("ERP_Link", inDto.getString("ERP_Link"));
            tcDto.put("MB003",dbname);
            tcDto.put("TC002", DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMdd));
            dd01 = this.baseMapper.getMaxTc002(tcDto);
            if (dd01 == null) {
                dd01 = DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMdd) + "001";
            }

            //判断订单是否已经抛过  都是整单抛转 不需要项次
            Dto inDto3 = Dtos.newDto();
            inDto3.put("MB003", dbname);
            inDto3.put("ERP_Link", ERP_Link);
            inDto3.put("TC065", mmrx_tPOs.get(0).getString("MMRXDOCNO"));
            Dto checkinfo = this.baseMapper.getTC065(inDto3);
            if (checkinfo != null) {
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));//客户编号
                jo.put("GWAKL003",  mmrx_tPOs.get(0).getString("MMRX018_NAME"));//客户名称
                jo.put("MMRXDOCNO",  mmrx_tPOs.get(0).getString("MMRXDOCNO"));//订单号
                json.add(jo);
                //该订单已经抛转过  同步失败  跳过进行下个订单
                continue;
            }

            //判断客户编号在ERP中是否存在 不存在就新增一笔数据
            Dto inDto2 = Dtos.newDto();
            inDto2.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));
            inDto2.put("MB003", dbname);
            inDto2.put("ERP_Link", inDto.getString("ERP_Link"));
            Dto outDto2 = this.baseMapper.selectByGwak001(inDto2);
            if (outDto2 == null || outDto2.getString("MA001").isEmpty()) {
                String syncresult = this.SyncERP(mmrx_tPOs.get(0).getString("MMRX018"), inDto.getString("ERP_Link"),dbname);
                if (syncresult.equals("N")) {
                    JSONObject jo = new JSONObject();
                    jo.put("ERPRESULT", "N");//同步结果  Y  N
                    jo.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));//客户编号
                    jo.put("GWAKL003",  mmrx_tPOs.get(0).getString("MMRX018_NAME"));//客户名称
                    jo.put("MMRXDOCNO",  mmrx_tPOs.get(0).getString("MMRXDOCNO"));//订单号
                    json.add(jo);
                    //同步客户资料失败  整张订单跳过 进行下个订单
                    continue;
                }

            }

            String TD001 = "";
            String seq = "";
            String TD002 = "";

            try {
                int count = 1;
                for (Dto mmrx_tPO : mmrx_tPOs) {
                    //单身信息
                    Dto qDto = Dtos.newDto();
                    qDto.put("ERP_Link", inDto.getString("ERP_Link"));
                    qDto.put("MB003", dbname);//目标数据库
                    qDto.put("COMPANY",mmrx_tPO.getString("MMRXPLANT"));
                    //按法人组织抓取单别
                    Dto dhinfo = this.mmrxTService.getGwaz(mmrx_tPO.getString("MMRXLEGAL"));
                    qDto.put("TD001", dhinfo.getString("GWAZ002"));
                    TD001 = dhinfo.getString("GWAZ002");
                    qDto.put("TD002",dd01);
                    DecimalFormat mFormat = new DecimalFormat("0000");//确定格式，把1 转换为0001
                    String mmrxseq = mFormat.format(mmrx_tPO.getBigDecimal("MMRXSEQ"));
                    seq = mFormat.format(mmrx_tPO.getBigDecimal("MMRXSEQ"));
                    qDto.put("TD003", mmrxseq);
                    qDto.put("TD004", mmrx_tPO.getString("MMRX001"));//取CRM中购物车的品号(需要判断是否在ERP中是否存在?）
                    qDto.put("TD005", mmrx_tPO.getString("GWAML003"));
                    qDto.put("TD006", mmrx_tPO.getString("GWAML004"));
                    qDto.put("TD007", mmrx_tPO.getString("GWAM019"));
                    qDto.put("TD008", mmrx_tPO.getString("MMRX003"));
                    qDto.put("TD009", 0);
                    qDto.put("TD010", mmrx_tPO.getString("MMRX002"));
                    qDto.put("TD011", mmrx_tPO.getString("MMRX004"));
                    qDto.put("TD012", mmrx_tPO.getString("MMRX005"));
                    qDto.put("TD013", "");
                    qDto.put("TD016", "N");
                    qDto.put("TD020", mmrx_tPO.getString("MMRX031"));
                    qDto.put("TD021", "N");
                    qDto.put("TD022", 0);
                    qDto.put("TD024", 0);
                    qDto.put("TD025", 0);
                    qDto.put("TD026", 1);
                    qDto.put("TD030", 0);
                    qDto.put("TD031", 0);
                    qDto.put("TD032", 0);
                    qDto.put("TD033", 0);
                    qDto.put("TD034", 0);
                    qDto.put("TD035", 0);
                    if (mmrx_tPO.getBigDecimal("GWEA006") == null || mmrx_tPO.getBigDecimal("GWEA006").equals(new BigDecimal(0.00))) {
                        qDto.put("TD037", 0.16);
                    } else {
                        qDto.put("TD037", mmrx_tPO.getBigDecimal("GWEA006").divide(new BigDecimal(100), 4, RoundingMode.HALF_UP));//根据税种mmrx007抓税率
                    }
                    if (mmrx_tPO.getBigDecimal("TD038") == null) {
                        qDto.put("TD038", "");//mmrx014-mmrx016
                    } else {
                        qDto.put("TD038", mmrx_tPO.getBigDecimal("TD038").setScale(2, RoundingMode.HALF_UP));//mmrx014-mmrx016
                    }
                    if (mmrx_tPO.getBigDecimal("MMRX015") == null) {
                        qDto.put("TD039", "");
                    } else {
                        qDto.put("TD039", mmrx_tPO.getBigDecimal("MMRX015").setScale(2, RoundingMode.HALF_UP));
                    }
                    qDto.put("TD040", 0);
                    qDto.put("TD041", 0);
                    qDto.put("TD042", 0);
                    qDto.put("TD043", 0);
                    qDto.put("TD046", 0);
                    qDto.put("TD047", 0);
                    qDto.put("TD048", 0);
                    qDto.put("TD049", 0);
                    qDto.put("TD050", 0);
                    qDto.put("TD062", "N");
                    qDto.put("TD200", 0);
                    qDto.put("TDD01", "N");

                    if (mmrx_tPO.getBigDecimal("TDI01") == null) {
                        qDto.put("TDI01", "");//mmrx014-mmrx016
                    } else {
                        qDto.put("TDI01", mmrx_tPO.getBigDecimal("TDI01").setScale(2, RoundingMode.HALF_UP));//gwcs039-gwcs041
                    }
                    if (mmrx_tPO.getBigDecimal("MMRX016") == null) {
                        qDto.put("TDI02", "");
                    } else {
                        qDto.put("TDI02", mmrx_tPO.getBigDecimal("MMRX016").setScale(2, RoundingMode.HALF_UP));
                    }
                    if (mmrx_tPO.getBigDecimal("MMRX014") == null) {
                        qDto.put("TDI03", "");
                    } else {
                        qDto.put("TDI03", mmrx_tPO.getBigDecimal("MMRX014").setScale(2, RoundingMode.HALF_UP));
                    }
                    qDto.put("UDF51", 0);
                    qDto.put("UDF52", 0);
                    qDto.put("UDF53", 0);
                    qDto.put("UDF54", 0);
                    qDto.put("UDF55", 0);
                    qDto.put("UDF56", 0);
                    qDto.put("UDF57", 0);
                    qDto.put("UDF58", 0);
                    qDto.put("UDF59", 0);
                    qDto.put("UDF60", 0);
                    qDto.put("UDF61", 0);
                    qDto.put("UDF62", 0);
                    qDto.put("CREATOR",shiroUser.getBIZ_CODE());
                    qDto.put("USR_GROUP", "");
                    qDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMdd"));
                    qDto.put("FLAG", 1);

                    count++;
                    this.baseMapper.insertCOPTD(qDto);

                    MmrxT mmrxT = new MmrxT();
                    mmrxT.setMMRX029(dd01);
                    mmrxT.setMMRXACTI("Z");
                    UpdateWrapper<MmrxT> mmrxTUpdateWrapper = new UpdateWrapper<>();
                    mmrxTUpdateWrapper.eq("MMRXSENT", shiroUser.getDBC());
                    mmrxTUpdateWrapper.eq("MMRXDOCNO", mmrx_tPO.getString("MMRXDOCNO"));
                    mmrxTUpdateWrapper.eq("MMRXSEQ", mmrx_tPO.getString("MMRXSEQ"));
                    mmrxT.update(mmrxTUpdateWrapper);
                }
            } catch (Exception e) {
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));//客户编号
                jo.put("GWAKL003",  mmrx_tPOs.get(0).getString("MMRX018_NAME"));//客户名称
                jo.put("MMRXDOCNO",  mmrx_tPOs.get(0).getString("MMRXDOCNO"));//订单号
                json.add(jo);

                inDto.put("COMPANY",mmrx_tPOs.get(0).getString("MMRXPLANT"));
                inDto.put("TD049",0);
                inDto.put("MB003", dbname);
                inDto.put("TD001", TD001);
                inDto.put("TD002", dd01);
                this.baseMapper.deleteCOPTD(inDto);

                MmrxT mmrxT = new MmrxT();
                mmrxT.setMMRX029("");
                mmrxT.setMMRXACTI("Y");
                UpdateWrapper<MmrxT> mmrxTUpdateWrapper = new UpdateWrapper<>();
                mmrxTUpdateWrapper.eq("MMRXSENT", shiroUser.getDBC());
                mmrxTUpdateWrapper.eq("MMRXDOCNO", mmrx_tPOs.get(0).getString("MMRXDOCNO"));
                mmrxT.update(mmrxTUpdateWrapper);

                //插入COPTD 失败  整张订单跳过 进行下个订单
                e.printStackTrace();
                continue;
            }

            try {
                //单头  订单信息
                Dto mmrx_tPO = mmrx_tPOs.get(0);
                Dto pDto = Dtos.newDto();

                pDto.put("ERP_Link", inDto.getString("ERP_Link"));
                pDto.put("MB003", dbname);//目标数据库
                //按法人组织抓取单号
                Dto dhinfo = this.mmrxTService.getGwaz(mmrx_tPO.getString("MMRXLEGAL"));
                pDto.put("COMPANY",mmrx_tPO.getString("MMRXPLANT"));
                pDto.put("TC001", dhinfo.getString("GWAZ002"));
                pDto.put("TC002", dd01);
                pDto.put("TC003", DateUtils.getDate("yyyyMMdd"));
                pDto.put("TC004", mmrx_tPO.getString("MMRX018"));
                pDto.put("TC005", mmrx_tPO.getString("MMRX032_ERP"));
                pDto.put("TC006", mmrx_tPO.getString("MMRX022_ERP"));
                pDto.put("TC007", "01");
                pDto.put("TC008", mmrx_tPO.getString("MMRX006"));
                pDto.put("TC009", mmrx_tPO.getString("MMRX009"));
                if(mmrx_tPO.getString("MMRX025").split(";").length > 0){
                    pDto.put("TC010", mmrx_tPO.getString("MMRX025").split(";")[0]);
                }
                pDto.put("TC011", mmrx_tPO.getString("MMRX020")+ mmrx_tPO.getString("MMRX021"));
                pDto.put("TC015", mmrx_tPO.getString("MMRX031"));
                pDto.put("TC016", "1");
                pDto.put("TC018", mmrx_tPO.getString("MMRX022"));
                pDto.put("TC019", "1");
                pDto.put("TC026", 0);
                pDto.put("TC027", "N");
                pDto.put("TC028", 0);

                Dto sumData = this.mmrxTService.getSumData(inDto);
                pDto.put("TC029", sumData.getBigDecimal("MMRX014_SUM"));//sum（gwcs036-gwcs040）
                pDto.put("TC030", sumData.getBigDecimal("MMRX016_SUM"));//sum（gwcs040）
                pDto.put("TC031", sumData.getBigDecimal("MMRX003_SUM"));//sum（gwcs003）
                pDto.put("TC039", DateUtils.getDate("yyyyMMdd"));
                if (mmrx_tPO.getBigDecimal("GWEA006") == null || mmrx_tPO.getBigDecimal("GWEA006").equals(new BigDecimal(0.00))) {
                    pDto.put("TC041", 0.16);
                } else {
                    pDto.put("TC041", mmrx_tPO.getBigDecimal("GWEA006").divide(new BigDecimal(100), 4, RoundingMode.HALF_UP));//根据税种gwcs032抓税率
                }
                pDto.put("TC043", 0);
                pDto.put("TC044", 0);
                pDto.put("TC045", 1);
                pDto.put("TC046", 0);
                pDto.put("TC048", "N");
                pDto.put("TC050", "N");
                //发票信息
                if(mmrx_tPO.getString("MMRX026").split(";").length > 0){
                    pDto.put("TC052", mmrx_tPO.getString("MMRX026").split(";")[0]);
                    pDto.put("TC053", mmrx_tPO.getString("MMRX026").split(";")[1] + " " + mmrx_tPO.getString("MMRX026").split(";")[2]);
                }
                pDto.put("TC054", mmrx_tPO.getString("MMRX018_NAME"));//客户名称
                pDto.put("TC058", 0);
                pDto.put("TC059", "N");
                pDto.put("TC062", 0);
                pDto.put("TC063", 0);
                pDto.put("TC064", 0);
                pDto.put("TC065", mmrx_tPO.getString("MMRXDOCNO"));
                pDto.put("TC066", "0");
                pDto.put("TC067", "2");
                pDto.put("TC068", 0);
                pDto.put("TC069", 0);
                pDto.put("TC070", "N");
                pDto.put("TC071", "0");
                pDto.put("TC073", mmrx_tPO.getString("GWAK007"));//按经销商mmrx019去客户档gwak抓税号gwak007
                pDto.put("TC074", mmrx_tPO.getString("MMRX021"));
//                pDto.put("TC200", "N");
//                pDto.put("TC201", "N");
//                pDto.put("TC202", "N"); 所选择的数据库中没有
                pDto.put("TCI01", DateUtils.getDate("yyyyMMdd"));
                pDto.put("TCI02", DateUtils.getDate("yyyyMMdd"));
                pDto.put("TCI03", sumData.getBigDecimal("MMRX014_SUM").subtract(sumData.getBigDecimal("MMRX016_SUM")));//sum(gwcs039-gwcs041)
                pDto.put("TCI04", sumData.getBigDecimal("MMRX016_SUM"));//sum(gwcs041)
                    /*pDto.put("UDF07", gwcs_tPO.getString("gwadl004_qb"));
                    pDto.put("UDF08", gwcs_tPO.getString("gwadl004_sb"));
                    pDto.put("UDF09", gwcs_tPO.getString("gwak054") + "," + gwcs_tPO.getString("gwagl003"));
                    pDto.put("UDF11", gwcs_tPO.getString("gwakl003_kh"));*/
                pDto.put("UDF12", "返利");
                pDto.put("UDF51", 0);
                pDto.put("UDF52", 0);
                pDto.put("UDF53", 0);
                pDto.put("UDF54", 0);
                pDto.put("UDF55", 0);
                pDto.put("UDF56", 0);
                pDto.put("UDF57", 0);
                pDto.put("UDF58", 0);
                pDto.put("UDF59", 0);
                pDto.put("UDF60", 0);
                pDto.put("UDF61", 0);
                pDto.put("UDF62", 0);
                pDto.put("CREATOR", shiroUser.getBIZ_CODE());
                pDto.put("USR_GROUP", "");
                pDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMdd"));
                pDto.put("FLAG", 1);

                int row = this.baseMapper.insertCOPTC(pDto);

                String result = "N";
                if (row > 0) {
                    result = "Y";
                }
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", result);//同步结果  Y  N
                jo.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));//客户编号
                jo.put("GWAKL003",  mmrx_tPOs.get(0).getString("MMRX018_NAME"));//客户名称
                jo.put("MMRXDOCNO",  mmrx_tPOs.get(0).getString("MMRXDOCNO"));//订单号
                json.add(jo);
            } catch (Exception e) {
                JSONObject jo = new JSONObject();
                jo.put("ERPRESULT", "N");//同步结果  Y  N
                jo.put("GWAK001", mmrx_tPOs.get(0).getString("MMRX018"));//客户编号
                jo.put("GWAKL003",  mmrx_tPOs.get(0).getString("MMRX018_NAME"));//客户名称
                jo.put("MMRXDOCNO",  mmrx_tPOs.get(0).getString("MMRXDOCNO"));//订单号
                json.add(jo);

                inDto.put("MB003", dbname);
                inDto.put("TD001", TD001);
                inDto.put("TD002", dd01);
                this.baseMapper.deleteCOPTD(inDto);

                MmrxT mmrxT = new MmrxT();
                mmrxT.setMMRX029("");
                mmrxT.setMMRXACTI("Y");
                UpdateWrapper<MmrxT> mmrxTUpdateWrapper = new UpdateWrapper<>();
                mmrxTUpdateWrapper.eq("MMRXSENT", shiroUser.getDBC());
                mmrxTUpdateWrapper.eq("MMRXDOCNO", data);
                mmrxT.update(mmrxTUpdateWrapper);

                e.printStackTrace();
                continue;
            }
        }
        return json;
    }

    /**
     * 还原订单(gwei025)
     */
    public Dto restore025(Dto inDto) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = inDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);
        int rows = 0;
        for (String data : rid) {
            inDto.put("MMRXDOCNO", data);
            inDto.put("TC065", data);

            //根据单号查询维修费用结算表数据
            List<Dto> mmrxData = this.mmrxTService.getMmrxAllData(inDto);
            //根据订单上的法人组织字段mmrwlegal 从sys_org表找到ERP组织 根据ERP组织=MB001，从ERP的数据库DSCSYS的表DSCMB中抓到MB003的值
            Dto dbinfo1 = Dtos.newDto();
            dbinfo1.put("ERP_Link",inDto.getString("ERP_Link"));
            dbinfo1.put("ERP_DB",inDto.getString("ERP_DB"));
            dbinfo1.put("BIZ_CODE",mmrxData.get(0).getString("MMRXPLANT"));
            Dto dbinfo2 = this.baseMapper.getDBinfo2(dbinfo1);
            String dbname = dbinfo2.getString("MB003").trim();

            inDto.put("TC027", "V");
            inDto.put("MB003", dbname);
            Dto COPTCinfo = this.baseMapper.getCOPTC(inDto);
            if (COPTCinfo == null) {
                MmrxT mmrxT = new MmrxT();
                mmrxT.setMMRXACTI("Y");
                mmrxT.setMMRX029("");
                UpdateWrapper mmrxTUpdateWrapper = new UpdateWrapper();
                mmrxTUpdateWrapper.eq("MMRXSENT",shiroUser.getDBC());
                mmrxTUpdateWrapper.eq("MMRXDOCNO",data);
                mmrxT.update(mmrxTUpdateWrapper);
                outDto.put("docno", "找不到对应ERP订单");
            } else if (COPTCinfo.getString("TC027").equals("N")) {
                Dto inDto2 = Dtos.newDto();
                inDto2.put("ERP_Link", inDto.getString("ERP_Link"));
                inDto2.put("MB003", dbname);
                inDto2.put("TC027", "V");
                inDto2.put("TC002", COPTCinfo.getString("TC002"));
                this.baseMapper.updateCOPTC(inDto2);

                MmrxT mmrxT = new MmrxT();
                mmrxT.setMMRXACTI("Y");
                mmrxT.setMMRX029("");
                UpdateWrapper mmrxTUpdateWrapper = new UpdateWrapper();
                mmrxTUpdateWrapper.eq("MMRXSENT",shiroUser.getDBC());
                mmrxTUpdateWrapper.eq("MMRXDOCNO",data);
                mmrxT.update(mmrxTUpdateWrapper);
                outDto.put("docno", "操作成功");
            } else if (COPTCinfo.getString("TA006").equals("Y")) {
                outDto.put("docno", "ERP单据已经审核不能还原");
            }
            rows++;
        }
        return outDto;
    }

    /**
     * 审核订单变更单(gwai770)
     */
    public Dto checkGwai770(Dto inDto){
        Dto outDto = Dtos.newDto();
        outDto.put("code", 200);
        List<GwdfT> gwdfTList = this.gwdfTService.getGwdfData(inDto);
        List<Dto> change = new ArrayList();
        int count_1 = 0;
        int count_2 = 0;
        for (GwdfT gwdfT : gwdfTList) {
            Dto queryDto = Dtos.newDto();
            queryDto.put("MB001", gwdfT.getGWDFMLEGAL());
            inDto.put("MB001", gwdfT.getGWDFMLEGAL());
            queryDto.put("XMDH001", gwdfT.getGWDF003());//订单项次
            queryDto.put("XMDH002", gwdfT.getGWDF004()); //订单项次
            queryDto.put("ERP_Link", inDto.getString("ERP_Link"));
            Dto sumTh008 = this.baseMapper.getSumTh008(queryDto);
            if (sumTh008 == null) {
                sumTh008 = Dtos.newDto();
                sumTh008.put("TH008_SUM", 0);
            }
            if (sumTh008.getBigDecimal("TH008_SUM").compareTo(gwdfT.getGWDF015()) == 0) {
                if (gwdfT.getGWDF015().compareTo(gwdfT.getGWDF014()) < 1) {
                    continue;
                } else {
                    count_2++;
                }
            } else {
                Dto changeDto = Dtos.newDto();
                changeDto.put("GWDF003", gwdfT.getGWDF003());
                changeDto.put("GWDF004", gwdfT.getGWDF004());
                changeDto.put("GWDF015", sumTh008.getBigDecimal("TH008_SUM"));
                change.add(changeDto);
                count_1++;
            }
        }
        if (count_1 > 0) {
            outDto.put("code", 300);
            outDto.put("change_list", change);
        } else if (count_2 > 0) {
            outDto.put("code", 400);
        } else {//修改分销订单及ERP订单数据
            for (GwdfT gwdfT : gwdfTList) {
                if (gwdfT.getGWDF004() == null || ("").equals(gwdfT.getGWDF004())) {
                    //查询ERP数据，获取该单号最大项次
                    Dto maxTD003 = this.gwdfTService.getMaxTD003(gwdfT.getGWDFMLEGAL(), gwdfT.getGWDF003());
                    Dto gwamQ = Dtos.newDto();
                    gwamQ.put("ENT", inDto.getString("ENT"));
                    gwamQ.put("LANG", inDto.getString("LANG"));
                    gwamQ.put("GWAM001", gwdfT.getGWDF011());
                    Dto gwam = this.gwdfTService.getGwam(gwamQ);
                    GwcsT gwcsTQ = this.gwdfTService.getMaxGwcsseq(gwdfT.getGWDF001());
                    try {
                        this.gwdfTService.addGwcs(gwcsTQ, gwdfT);
                        this.insertCOPTD(maxTD003, gwdfT, gwam, gwcsTQ.getGWCSSEQ().add(new BigDecimal(1)));
                    } catch (Exception e) {
                        BigDecimal TD003 = maxTD003.getBigDecimal("TD003_MAX").add(new BigDecimal(1));
                        DecimalFormat df = new DecimalFormat("0000");
                        String td003 = df.format(TD003);
                        Dto delCoptc = Dtos.newDto();
                        delCoptc.put("MB001", gwdfT.getGWDFMLEGAL());
                        delCoptc.put("TD002", gwdfT.getGWDF003());//订单号
                        delCoptc.put("TD003", td003);//订单项次
                        delCoptc.put("ERP_Link", inDto.getString("ERP_Link"));
                        this.gwdfTService.delGwcs(gwcsTQ);
                        this.baseMapper.delCOPTD(delCoptc);
                        this.baseMapper.updateCOPTCDEL(delCoptc);
                        e.printStackTrace();
                        outDto.put("code", 500);
                        outDto.put("message", "订单变更单确认失败！" + e.getMessage());
                    }
                }
                //修改ERP数据
                Dto COPTD = Dtos.newDto();
                COPTD.put("ERP_Link", inDto.getString("ERP_Link"));
                COPTD.put("MB001", gwdfT.getGWDFMLEGAL());
                COPTD.put("TC038", gwdfT.getGWDF031());//备注
                COPTD.put("UDF12", gwdfT.getGWDF007());//返利类型
                switch (gwdfT.getGWDF007()) {
                    case "1":
                        COPTD.put("UDF12", "现款有返利");
                        break;
                    case "2":
                        COPTD.put("UDF12", "现款无返利");
                        break;
                    case "3":
                        COPTD.put("UDF12", "帐期有返利");
                        break;
                    case "4":
                        COPTD.put("UDF12", "帐期无返利");
                        break;
                    case "5":
                        COPTD.put("UDF12", "铺货有返利");
                        break;
                    case "6":
                        COPTD.put("UDF12", "铺货无返利");
                        break;
                }
                COPTD.put("TD008", gwdfT.getGWDF014());//订单数量
                COPTD.put("TD007", gwdfT.getGWDF023());//仓库
                //COPTD.put("TD011", gwdfT.getGWDF017());//价格
                COPTD.put("TD012", gwdfT.getGWDF026());//金额
                COPTD.put("TD039", gwdfT.getGWDF029());//税额
                COPTD.put("TD038", gwdfT.getGWDF026().subtract(gwdfT.getGWDF029()));//税前金额
                COPTD.put("TDI01", COPTD.getBigDecimal("TD038").multiply(gwdfT.getGWDF024()));//本币税前金额
                COPTD.put("TDI02", gwdfT.getGWDF029().multiply(gwdfT.getGWDF024()));//本币税前金额
                COPTD.put("TDI03", gwdfT.getGWDF026().multiply(gwdfT.getGWDF024()));//本币金额
                COPTD.put("TD002", gwdfT.getGWDF003());//订单号
                COPTD.put("TD003", gwdfT.getGWDF004());//订单项次
                COPTD.put("TD026", gwdfT.getGWDF036());//折扣率

                //更新分销订单数据
                GwcsT gwcsT = new GwcsT();
                //gwcsT.setGWCS050(gwdfT.getGWDF031());//备注
                //gwcsT.setGWCS117(gwdfT.getGWDF007());//返利类型
                gwcsT.setGWCS003(gwdfT.getGWDF014());//订单数量
                gwcsT.setGWCS113(gwdfT.getGWDF023());//仓库
                gwcsT.setGWCS004(gwdfT.getGWDF017());//价格
                gwcsT.setGWCS005(gwdfT.getGWDF026());//金额
                gwcsT.setGWCS040(gwdfT.getGWDF029());//税额
                gwcsT.setGWCS112(gwdfT.getGWDF021());//仓库类型
                gwcsT.setGWCS035(gwdfT.getGWDF036());//折扣
                gwcsT.setGWCSDOCNO(gwdfT.getGWDF001());//订单号
                gwcsT.setGWCSSEQ(gwdfT.getGWDF005());//订单项次
                gwcsT.setGWCSENT(gwdfT.getGWDFENT());

                GwcsT gwcsTNew = new GwcsT();
                gwcsTNew.setGWCS117(gwdfT.getGWDF007());//返利类型
                gwcsTNew.setGWCSDOCNO(gwdfT.getGWDF001());//订单号
                gwcsTNew.setGWCS050(gwdfT.getGWDF031());//备注
                gwcsTNew.setGWCSENT(gwdfT.getGWDFENT());

                //更新赠品
                GwcszT gwcszT = new GwcszT();
                gwcszT.setGWCSZENT(gwdfT.getGWDFENT());
                gwcszT.setGWCSZDOCNO(gwdfT.getGWDF001());
                gwcszT.setGWCSZSEQ(gwdfT.getGWDF004());//erp变更单项序
                gwcszT.setGWCSZ005(gwdfT.getGWDF014());//价格
                try {
                    this.gwdfTService.updateGWCS_GWCSZ(gwcsT, gwcszT, gwcsTNew);
                    this.baseMapper.updateCOPTD(COPTD);
                    this.baseMapper.updateCOPTCSum(COPTD);
                } catch (Exception e) {
                    e.printStackTrace();
                    outDto.put("code", 500);
                    outDto.put("message", "订单变更单确认失败！" + e.getMessage());
                }

            }
            this.gwdfTService.updateGwdf(inDto);
        }
        return outDto;
    }

    public void insertCOPTD(Dto maxTD003, GwdfT gwdfT, Dto gwam, BigDecimal gwcsseq) {
        BigDecimal TD003 = maxTD003.getBigDecimal("TD003_MAX").add(new BigDecimal(1));
        DecimalFormat df = new DecimalFormat("0000");
        String td003 = df.format(TD003);
        Dto COPTD = Dtos.newDto();
        COPTD.put("MB001", gwdfT.getGWDFMLEGAL());//目标数据库
        COPTD.put("TD001", "FXDD");
        COPTD.put("TD002", gwdfT.getGWDF003());
        COPTD.put("TD003", td003);
        COPTD.put("TD004", gwdfT.getGWDF011());//品号
        if (gwam != null) {
            COPTD.put("TD005", gwam.getString("GWAML003"));
            COPTD.put("TD006", gwam.getString("GWAML004"));
        }
        //COPTD.put("TD007", gwdfT.getGWDF023());//仓库
        //COPTD.put("TD008", gwdfT.getGWDF014());//数量
        COPTD.put("TD009", 0);
        COPTD.put("TD010", gwdfT.getGWDF012());//单位
        //COPTD.put("TD011", gwdfT.getGWDF017());//单价
        //COPTD.put("TD012", gwdfT.getGWDF026());//金额
        //COPTD.put("TDI03", gwdfT.getGWDF026().multiply(maxTD003.getBigDecimal("TC009")));
        COPTD.put("TD013", maxTD003.getString("TD013"));
        COPTD.put("TD016", "N");
        COPTD.put("TD021", "Y");
        //COPTD.put("TD026", gwdfT.getGWDF036());
        Dto mbDto = Dtos.newDto();
        mbDto.put("COMPANY", gwdfT.getGWDFMLEGAL());
        mbDto.put("MB001", gwdfT.getGWDF011());
        Dto mb = this.gwcsTService.getMB(mbDto);
        if (mb != null) {
            COPTD.put("TD036", mb.getString("MB090"));//包装单位
            mbDto.put("TD036", mb.getString("MB090"));
            Dto tDto = this.gwcsTService.getTD032(mbDto);
            if (mb != null && tDto != null) {
                //COPTD.put("TD032", gwdfT.getGWDF014().multiply(tDto.getBigDecimal("TD032")));//包装数量
                COPTD.put("TD032", tDto.getBigDecimal("TD032"));//包装数量
            } else {
                COPTD.put("TD032", 0);
            }
        } else {
            COPTD.put("TD032", 0);
        }
        COPTD.put("TD037", gwdfT.getGWDF010());//税率
        //COPTD.put("TD038", gwdfT.getGWDF026().subtract(gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP)));
        //COPTD.put("TDI01", (gwdfT.getGWDF026().subtract(gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP))).multiply(maxTD003.getBigDecimal("TC009")));//gwcs036-gwcs040
        //COPTD.put("TD039", gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP));
        COPTD.put("TDI02", (gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP)).multiply(maxTD003.getBigDecimal("TC009")));
        COPTD.put("TD048", gwcsseq);
        COPTD.put("TD049", gwdfT.getGWDF001());
        COPTD.put("TD062", "N");
        COPTD.put("TDD01", "N");
        COPTD.put("UDF07", COPTD.getString("TD004"));
        COPTD.put("UDF61", 0);
        COPTD.put("COMPANY", gwdfT.getGWDFMLEGAL());
        COPTD.put("FLAG", 1);

        //修改ERP单身数据
        //Dto COPTC = Dtos.newDto();
        COPTD.put("mlegal", gwdfT.getGWDFMLEGAL());
        COPTD.put("TC038", gwdfT.getGWDF031());//备注
        COPTD.put("UDF12", gwdfT.getGWDF007());//返利类型
        COPTD.put("TD008", gwdfT.getGWDF014());//订单数量
        COPTD.put("TD007", gwdfT.getGWDF023());//仓库
        COPTD.put("TD011", gwdfT.getGWDF016());//价格
        COPTD.put("TD012", gwdfT.getGWDF026());//金额
        COPTD.put("TD039", gwdfT.getGWDF029());//税额
        COPTD.put("TD038", gwdfT.getGWDF026().subtract(gwdfT.getGWDF029()));//税前金额
        COPTD.put("TDI01", COPTD.getBigDecimal("TD038").multiply(gwdfT.getGWDF024()));//本币税前金额
        COPTD.put("TDI02", gwdfT.getGWDF029().multiply(gwdfT.getGWDF024()));//本币税前金额
        COPTD.put("TDI03", gwdfT.getGWDF026().multiply(gwdfT.getGWDF024()));//本币金额
        //COPTC.put("TD002",gwdfT.getGWDF003());//订单号
        //COPTC.put("TD003",gwdfT.getGWDF004());//订单项次
        COPTD.put("TD026", gwdfT.getGWDF036());//折扣率

        this.baseMapper.insertCOPTD(COPTD);
        this.baseMapper.updateCOPTCSum(COPTD);
    }

    public JSONArray insertErpGwak(Dto inDto) {
        String[] gwak001s = inDto.getString("gwak001_list").split(",");
        String[] MB001s = inDto.getString("mb001_list").split(",");
        ShiroUser shiroUser = ShiroKit.getUser();
        // 数据库主机
        String ERP_Link = inDto.getString("ERP_Link");
        JSONArray json = new JSONArray();
        for (String gwak001 : gwak001s) {
            //一条客户资料数据
            Dto gwakDto = Dtos.newDto();
            gwakDto.put("ent", shiroUser.getDBC());
            gwakDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
            gwakDto.put("gwak001", gwak001);
            Dto info = gwakTService.getGwakData(gwakDto);
            String dbname = info.getString("GWCSPLANT");
            for (String MB001 : MB001s) {
                //是否写入成功
                String result = "N";
                if (MB001 != null) {
                    //判断客户编号在ERP中是否存在 不存在就新增一笔客户数据
                    Dto inDto2 = Dtos.newDto();
                    inDto2.put("GWAK001", gwak001);
                    inDto2.put("MB003", dbname);
                    inDto2.put("ERP_Link", inDto.getString("ERP_Link"));
                    Dto outDto2 = this.baseMapper.selectByGwak001(inDto2);
                    int count = 0;
                    //  客户不存在，将客户资料同步到ERP
                    if (outDto2 == null || outDto2.getString("MB001").isEmpty()) {
                        try {
                            count = this.gwakData(inDto2.getString("MB001"), ERP_Link, dbname, "add");
                        } catch (Exception e) {
                            JSONObject jo = new JSONObject();
                            jo.put("SYNCRESULT", result);//同步结果  Y  N syncresult
                            jo.put("GWAK001", info.getString("GWAK001"));//客户编号
                            jo.put("GWAKL003", info.getString("GWAKL003"));//客户名称
                            jo.put("REMARK", e.getMessage());
                            if (MB001 != null) {
                                jo.put("MB002", MB001);//erp公司
                            } else {
                                jo.put("MB002", "");//erp公司
                            }
                            json.add(jo);
                            continue;
                        }
                    } else {
                        try {
                            count = this.gwakData(inDto2.getString("MB001"), ERP_Link, dbname, "update");
                        } catch (Exception e) {
                            JSONObject jo = new JSONObject();
                            jo.put("SYNCRESULT", result);//同步结果  Y  N syncresult
                            jo.put("GWAK001", info.getString("GWAK001"));//客户编号
                            jo.put("GWAKL003", info.getString("GWAKL003"));//客户名称
                            jo.put("REMARK", e.getMessage());
                            if (MB001 != null) {
                                jo.put("MB002", MB001);//erp公司
                            } else {
                                jo.put("MB002", "");//erp公司
                            }
                            json.add(jo);
                            continue;
                        }
                    }
                    if (count > 0) {
                        result = "Y";
                    }
                }
                JSONObject jo = new JSONObject();
                jo.put("SYNCRESULT", result);//同步结果  Y  N syncresult
                jo.put("GWAK001", info.getString("GWAK001"));//客户编号
                jo.put("GWAKL003", info.getString("GWAKL003"));//客户名称
                jo.put("REMARK", "");
                if (MB001 != null) {
                    jo.put("MB002", MB001);//erp公司
                } else {
                    jo.put("MB002", "");//erp公司
                }
                json.add(jo);
            }
        }
        return json;
    }

    public int gwakData(String gwak001, String ERP_Link, String MB003, String type){
        ShiroUser shiroUser = ShiroKit.getUser();
        int i=0;

        //获取gwak001对应的客户资料数据
        Dto qDto = Dtos.newDto();
        qDto.put("ent", shiroUser.getDBC());
        qDto.put("gwak001", gwak001);
        Dto info = this.gwakTService.getGwakData(qDto);

        //获取目标数据库
        //Dto dbinfo = this.gwakTService.getDBinfo(MB001);
        Dto outDto = Dtos.newDto();
        //outDto.put("MB001", dbinfo.getString("MB001").trim());//获取目标数据库
        outDto.put("ERP_Link",ERP_Link);
        outDto.put("MB003", MB003);
        outDto.put("MA001", info.getString("GWAK001"));
        outDto.put("MA002", info.getString("GWAKL003"));
        outDto.put("MA003", info.getString("GWAKL004"));
        outDto.put("MA004", info.getString("GWAK050"));
        outDto.put("MA005", info.getString("GWAK031"));//联系人
        outDto.put("MA006", info.getString("GWAK030"));//电话1
        //outDto.put("MA007", info.getString("GWAK050"));//电话2
        //outDto.put("MA010", info.getString("GWAK007"));
        if (info.getString("GWAK051").equals("NULL")) {
            outDto.put("MA011", "");
        } else {
            outDto.put("MA011", info.getBigDecimal("GWAK051"));
        }
        if (info.getString("GWAK052").equals("NULL")) {
            outDto.put("MA012", "");
        } else {
            outDto.put("MA012", info.getBigDecimal("GWAK052"));
        }
        outDto.put("MA014", info.getString("GWAK008"));
        outDto.put("MA017", info.getString("GWAK023"));
        outDto.put("MA018", info.getString("GWAK024"));
        outDto.put("MA019", info.getString("GWAK021"));
        outDto.put("MA023", info.getString("GWAK028"));//注册地址
        if (!(info.getString("GWAL018_fp").isEmpty())) {
            outDto.put("MA025", info.getString("GWAL018_fp"));//gwal018 where gwal003='I' 发票地址
        }
        if (!(info.getString("GWAL018_sh").isEmpty())) {
            outDto.put("MA027", info.getString("GWAL018_sh"));//gwal018 where gwal003='D' 送货地址
        }
        outDto.put("MA031", info.getString("GWAK060_NAME"));
        outDto.put("MA032", "N");
        outDto.put("MA033", 0);
        outDto.put("MA034", 0);
        outDto.put("MA035", "2");
        outDto.put("MA036", 0);
        outDto.put("MA037", "A");
        //outDto.put("MA038", info.getString("GWAK012"));//税种
        outDto.put("MA038", "1");//税种
        outDto.put("MA039", "1");
        outDto.put("MA041", "2");
        outDto.put("MA042", "1");
        outDto.put("MA044", 0);
        outDto.put("MA045", 0);
        outDto.put("MA047", "112201"); //账款科目
        //outDto.put("MA048", "1");
        outDto.put("MA059", 0);
        outDto.put("MA060", 0);
        outDto.put("MA061", 0);
        outDto.put("MA062", "");//地址
        outDto.put("MA066", "N");
        outDto.put("MA067", 0);
        outDto.put("MA074", "1121");
        outDto.put("MA075", "86");
        outDto.put("MA082", "N");
        outDto.put("MA083", info.getString("GWAK060"));//付款条件
        outDto.put("MA084", "N");
        outDto.put("MA086", "N");
        outDto.put("MA087", "2");
        outDto.put("MA088", "1");
        outDto.put("MA089", "1");
        outDto.put("MA090", 1);
        outDto.put("MA091", 1);
        outDto.put("MA092", 1);
        outDto.put("MA093", 1);
        outDto.put("MA094", 1);
        outDto.put("MA095", 0);
        outDto.put("MA096", "1");
        outDto.put("MA097", "1");
        //抓税率
        if (info.getString("GWEA006") == null || info.getString("GWAK004").isEmpty()) {
            outDto.put("MA101", 0.16);
        } else {
            outDto.put("MA101", info.getBigDecimal("GWEA006"));
        }
        Dto orgDto = this.gwcsTService.getSysOrg(shiroUser.getORG_ID());
        outDto.put("MA103", "1");
        outDto.put("MA104", 0);
        outDto.put("MA105", "220301");
        outDto.put("MA106", "N");
        outDto.put("MA109", 0);
        outDto.put("MA110", 0);
        outDto.put("MA111", 0);
        outDto.put("MA113", "N");
        //outDto.put("UDF01", info.getString("GWAK004"));
        outDto.put("COMPANY", info.getString("GWAKPLANT"));

        if (orgDto != null) {
            //outDto.put("COMPANY", orgDto.getString("BIZ_CODE"));
            //outDto.put("USR_GROUP", orgDto.getString("BIZ_CODE"));
        }
        //outDto.put("CREATOR", shiroUser.getBIZ_CODE());
        //outDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMddSSSSS"));
        outDto.put("FLAG", 1);

        //将客户资料插入指定ERP数据库
        if("add".equals(type)) {
            i = this.baseMapper.insertCOPMA(outDto);
        }else {
            i = this.baseMapper.updateCOPMA(outDto);
        }
        return i;
    }
}
